package huawei0814;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * Created by JiaJia on 2019/8/31 0001.
 */
public class BoysGirls {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            String str=scanner.nextLine();
            int k=scanner.nextInt();
            //System.out.println(countResult(str,k));
            System.out.println(countResult(str,k));
        }
        scanner.close();
    }
    private static String countResult(String str,int k){
        int len=str.length();
        StringBuilder sb=new StringBuilder(str);
        String newString=sb.append(str).append(str).toString();
        char[] chars=newString.toCharArray();
        StringBuilder sb2=new StringBuilder();
        //sb2.append(findIndex(chars,len)).append(" ").append(countLength(chars,k,len));
        sb2.append(findIndex(chars,len));
        return sb2.toString();
    }
    private static int findIndex(char[] chars,int len){
        HashMap<Integer,Integer> map=new HashMap<>();
        for (int i = len; i <=len+len-1 ; i++) {
            if (chars[i]=='b') {
                int left = i - 1, right = i + 1;
                while (left >= i - len + 1 && right <= i + len - 1) {
                    if (chars[left] == 'g') {
                        map.put(i, map.getOrDefault(i, 0) + 1);
                        left--;
                    }
                    if (chars[right] == 'g') {
                        map.put(i, map.getOrDefault(i, 0) + 1);
                        right--;
                    } /*else if (chars[left] == 'b' && chars[right] == 'b')
                        break;*/
                }
            }
        }
        int max=0,resultIndex=0;
        for (int index:map.keySet()) {
            if (map.get(index)>max){
                resultIndex=index;
            }
        }
        return resultIndex;
    }
    private static int countLength(char[] chars,int k,int len){
        int mmax=0;
        for (int i = len; i <=len+len-1 ; i++) {
            if (chars[i]=='g'){
                int count=0;
                for (int j = i+1; j <=i+len-1 ; j++) {
                    while (k-1>0){
                        if (chars[j]=='g'){
                            k--;
                        }
                    }
                    count=j-i+1;
                }
                mmax=Math.max(mmax,count);
            }
        }
        return mmax;
    }
}